package com.huawei.hms.support.api.push.b.c;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.appgallery.serverreqkit.api.bean.startup.StartupResponse;
import com.huawei.hms.android.HwBuildEx;
import com.huawei.hms.support.api.push.b.b.a;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.ResourceLoaderUtil;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.util.Date;

public class c {
    private static int a = 0;

    @TargetApi(26)
    public static synchronized void a(Context context, a aVar, String str) {
        synchronized (c.class) {
            if (!(context == null || aVar == null)) {
                int i;
                int i2;
                int i3;
                int i4;
                Notification a;
                HMSLog.d("PushSelfShowLog", " showNotification , the msg id = " + aVar.a());
                if (a == 0) {
                    a = (context.getPackageName() + new Date().toString()).hashCode();
                }
                int i5;
                if (TextUtils.isEmpty(aVar.e())) {
                    i = a + 1;
                    a = i;
                    i2 = a + 1;
                    a = i2;
                    i3 = a + 1;
                    a = i3;
                    i5 = a + 1;
                    a = i5;
                    i4 = i5;
                } else {
                    i = (aVar.i() + aVar.e()).hashCode();
                    i2 = a + 1;
                    a = i2;
                    i3 = a + 1;
                    a = i3;
                    i5 = a + 1;
                    a = i5;
                    i4 = i5;
                }
                HMSLog.d("PushSelfShowLog", "notifyId:" + i + ",openNotifyId:" + i2 + ",delNotifyId:" + i3 + ",alarmNotifyId:" + i4);
                if (com.huawei.hms.support.api.push.b.d.a.a()) {
                    a = a(context, aVar, str, i, i2, i3);
                } else {
                    a = null;
                }
                NotificationManager notificationManager = (NotificationManager) context.getSystemService("notification");
                if (!(notificationManager == null || a == null)) {
                    if (VERSION.SDK_INT >= 26) {
                        notificationManager.createNotificationChannel(new NotificationChannel("HwPushChannelID", context.getString(ResourceLoaderUtil.getStringId("hms_push_channel")), 3));
                    }
                    notificationManager.notify(i, a);
                    if (aVar.f() > 0) {
                        Intent intent = new Intent("com.huawei.intent.action.PUSH_DELAY_NOTIFY");
                        intent.putExtra("selfshow_info", aVar.c()).putExtra("selfshow_token", aVar.d()).putExtra("selfshow_event_id", StartupResponse.CHANNELNO_QUERY_FAILURE).putExtra("extra_encrypt_data", str).putExtra("selfshow_notify_id", i).setPackage(context.getPackageName()).setFlags(32);
                        a(context, intent, (long) aVar.f(), i4);
                        HMSLog.d("PushSelfShowLog", "setDelayAlarm alarmNotityId" + i4 + " and intent is " + intent.toURI());
                    }
                }
            }
        }
    }

    private static PendingIntent a(Context context, a aVar, String str, int i, int i2) {
        Intent intent = new Intent("com.huawei.intent.action.PUSH_DELAY_NOTIFY");
        intent.putExtra("selfshow_info", aVar.c()).putExtra("selfshow_token", aVar.d()).putExtra("selfshow_event_id", "1").putExtra("extra_encrypt_data", str).putExtra("selfshow_notify_id", i).setPackage(context.getPackageName()).setFlags(268435456);
        return PendingIntent.getBroadcast(context, i2, intent, HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR);
    }

    private static PendingIntent b(Context context, a aVar, String str, int i, int i2) {
        Intent intent = new Intent("com.huawei.intent.action.PUSH_DELAY_NOTIFY");
        intent.putExtra("selfshow_info", aVar.c()).putExtra("selfshow_token", aVar.d()).putExtra("selfshow_event_id", "2").putExtra("selfshow_notify_id", i).setPackage(context.getPackageName()).putExtra("extra_encrypt_data", str).setFlags(268435456);
        return PendingIntent.getBroadcast(context, i2, intent, HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR);
    }

    @TargetApi(26)
    public static Notification a(Context context, a aVar, String str, int i, int i2, int i3) {
        Builder builder = new Builder(context);
        a.a(context, builder, aVar);
        int i4 = context.getApplicationInfo().labelRes;
        builder.setTicker(aVar.k());
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);
        builder.setDefaults(1);
        if (aVar.m() == null || "".equals(aVar.m())) {
            builder.setContentTitle(context.getResources().getString(i4));
        } else {
            builder.setContentTitle(aVar.m());
        }
        builder.setContentText(aVar.k());
        builder.setContentIntent(a(context, aVar, str, i, i2));
        builder.setDeleteIntent(b(context, aVar, str, i, i3));
        Bitmap a = a.a(context, aVar);
        if (a != null) {
            builder.setLargeIcon(a);
        }
        if (VERSION.SDK_INT >= 26) {
            builder.setChannelId("HwPushChannelID");
        }
        a(context, builder, aVar);
        b(context, builder, aVar);
        return builder.getNotification();
    }

    @SuppressLint({"NewApi"})
    private static void a(Context context, Builder builder, a aVar) {
        if ("com.huawei.android.pushagent".equals(context.getPackageName())) {
            Bundle bundle = new Bundle();
            Object i = aVar.i();
            if (!TextUtils.isEmpty(i)) {
                bundle.putString("hw_origin_sender_package_name", i);
                builder.setExtras(bundle);
            }
        }
    }

    @SuppressLint({"NewApi"})
    private static void b(Context context, Builder builder, a aVar) {
        if (HwBuildEx.VERSION.EMUI_SDK_INT >= 11 && com.huawei.hms.support.api.push.b.d.a.b(context)) {
            Bundle bundle = new Bundle();
            String i = aVar.i();
            HMSLog.i("PushSelfShowLog", "the package name of notification is:" + i);
            if (!TextUtils.isEmpty(i)) {
                CharSequence a = com.huawei.hms.support.api.push.b.d.a.a(context, i);
                HMSLog.i("PushSelfShowLog", "the app name is:" + a);
                if (a != null) {
                    bundle.putCharSequence("android.extraAppName", a);
                }
            }
            builder.setExtras(bundle);
        }
    }

    public static void a(Context context, Intent intent, long j, int i) {
        try {
            HMSLog.d("PushSelfShowLog", "enter setDelayAlarm(intent:" + intent.toURI() + " interval:" + j + "ms, context:" + context);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService("alarm");
            if (alarmManager != null) {
                alarmManager.set(0, System.currentTimeMillis() + j, PendingIntent.getBroadcast(context, i, intent, HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR));
            }
        } catch (Exception e) {
            HMSLog.w("PushSelfShowLog", "set DelayAlarm error" + e.toString());
        }
    }
}
